// pages/add-share/index.js
var api = require('../../api.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgServer: api.default.img_server,

    form: {
      name: '',
      mobile: '',
    },
    img: "/images/img-share-un.png",
    agree: 0,
    orderId:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    console.log(options)
    that.setData({
      orderId: options.id,
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var page = this;
    var user_info = wx.getStorageSync("user_info");
    var store = wx.getStorageSync("store");
    var share_setting = wx.getStorageSync("share_setting");
    page.setData({
      user_info: user_info,
      store: store,
      share_setting: share_setting
    });

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
  formSubmit: function (e) {
    var page = this;
    var user_info = wx.getStorageSync("user_info");
    var my_user = wx.getStorageSync("my_info");
    page.data.form = e.detail.value;

    var test = e.detail.value;
    test.orderId = page.data.orderId;
    // Object.assign(test, { "test": "testValue" })
    console.log(test);

    if (page.data.form.deliverySn == undefined || page.data.form.deliverySn == '') {
      wx.showModal({
        content: "请填写物流单号!",
        showCancel: false,
      })
      return;
    }
 
   
    console.log(page.data.agree);
    var test = e.detail.value;
    console.log(test);
    app.request({
      url: api.order.deliver,
      method: 'POST',
      data: e.detail.value,
      success: function (res) {
        if (res.code == 200) {
          user_info.is_distributor = 2;
          wx.setStorageSync(
            "user_info", user_info
          );
          wx.navigateBack({
            delta: 1
          })
          
         
        } else {
          wx.showModal({
            content: res.msg,
            showCancel: false,
          })
        }
      }
    });
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  agreement: function () {
    wx.redirectTo({
      url: '/pages/xieyi/xieyi',
    })
    // var share_setting = wx.getStorageSync("share_setting");
    // wx.showModal({
    //   title: '供应商协议',
    //   content: "sdfzzfdfzfrfffg",
    //   showCancel:false,
    //   confirmText:"我已阅读",
    //   confirmColor:"#ff4544",
    //   success: function (res) {
    //     if (res.confirm) {
    //       console.log('用户点击确定')
    //     }
    //   }
    // });
  },
  agree: function () {
    var page = this;
    var agree = page.data.agree;
    if (agree == 0) {
      agree = 1;
      page.setData({
        img: "/images/img-share-agree.png",
        agree: agree
      });
    }
    else if (agree == 1) {
      agree = 0;
      page.setData({
        img: "/images/img-share-un.png",
        agree: agree
      });
    }
  },

  chooseImage: function (e) {
    var that = this
    var type = e.currentTarget.dataset.id
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        // tempFilePath可以作为img标签的src属性显示图片
        const tempFilePaths = res.tempFilePaths
        if (type == 1) {
          that.setData({
            license: tempFilePaths[0]
          })
        } else {
          that.setData({
            cert: tempFilePaths[0]
          })
        }
        that.uploadFileImage(tempFilePaths[0])
      }
    })
  },
  /**
   * 图片上传api调用
   */
  uploadFileImage(img) {
    wx.uploadFile({
      url: api.default.upload_image,//"/api/aliyun/oss/callback",
      filePath: img,
      name: 'file',
      success(res) {
        // tempFilePath可以作为img标签的src属性显示图片
        const tempFilePaths = res
      }
    })
  }
})